# coding: utf-8
from __future__ import unicode_literals

from common.rabbitmq import RabbitMQ
from common.log import Log


class Producer(object):
    """
    create by chenhj 2017/7/25
    消息生产者
    """

    def __init__(self, queue):
        self.queue = queue

    def send(self, body):
        """
        create by chenhj 2017/7/25
        投递消息到RabbitMQ
        :return:
        """
        channel = RabbitMQ().get_channel()
        channel.queue_declare(self.queue, durable=False)
        Log.info('queue:{}-body:{}'.format(self.queue, body))
        try:
            channel.basic_publish(
                exchange='',
                routing_key=self.queue,
                body=body,
            )
        finally:
            channel.close()

    def exchange_send(self, body, exchange_type='fanout'):
        """
        created by chenhj at 19/12/2017

        """
        channel = RabbitMQ().get_channel()
        channel.exchange_declare(
            self.queue, exchange_type=exchange_type, durable=False
        )
        Log.info('queue:{}-body:{}'.format(self.queue, body))
        try:
            channel.basic_publish(
                exchange=self.queue,
                routing_key='',
                body=body,
            )
        finally:
            channel.close()
